﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using ModelLibrary;
using System.Text;

namespace WebC
{
    public partial class DevPartAdd : clsBaseForm
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Ajax.Utility.RegisterTypeForAjax(typeof(DevPartAdd));
            if (!IsPostBack)
            {
                loadInfo();
            }
        }

        public string sFileName = GlobleClass.UpFileType.DevPartfile.ToString();//"Devfile";

        #region 载入信息

        public void loadInfo()
        {
            DeviceInfo deviceinfo = null;
            string sModelName = "";
            DataSet ds = null;
            try
            {
                deviceinfo = new DeviceInfo();
                //inniDdlData(ddlPayWay,buy.getEnumDdl(2));
                if (m_Id == 0)
                {
                    if (!checkModelRightById(193, ref sModelName, (clsBaseLib)deviceinfo))
                    {
                        errorUrl(string.Format("你没有“{0}”的权限,请与管理员联系！", sModelName));
                        return;
                    }
                }
                else
                {
                    if (Convert.ToInt32(Request["noedit"]) == 1)
                    {
                        if (!checkModelRightById(192, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    else
                    {
                        if (!checkModelRightById(195, ref sModelName, (clsBaseLib)deviceinfo))
                        {
                            errorUrl(sModelName);
                            return;
                        }
                    }
                    ds = deviceinfo.getDevPartInfoById(m_Id);
                    //hdselect.Value = buy.getDeviceSelectIds(m_Id);
                }
                sModelFullName = sModelName;

            }
            catch (Exception e)
            {
                errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (deviceinfo != null)
                {
                    deviceinfo.Dispose();
                }
            }
            if (m_Id > 0)
            {
                if (ds != null)
                {
                    DataTable dt = ds.Tables[0];
                    if (dt.Rows.Count == 1)
                    {
                        txtPartCode.Text = Convert.ToString(dt.Rows[0]["PartCode"]);
                        txtPartName.Text = Convert.ToString(dt.Rows[0]["PartName"]);
                        txtPartDanwei.Text = Convert.ToString(dt.Rows[0]["Jiliangdw"]);
                        txtPartSpec.Text = Convert.ToString(dt.Rows[0]["Xinghaogg"]);
                        txtPartWeight.Text = Convert.ToString(dt.Rows[0]["SingleWeight"]);
                        txtPartCailiao.Text = Convert.ToString(dt.Rows[0]["Caizhi"]);

                        //txtPartName.Text = Convert.ToDateTime(dt.Rows[0]["BuyDateTime"]).ToString("yyyy-MM-dd");
                        txtPartFactory.Text = Convert.ToString(dt.Rows[0]["Creater"]);
                        txtPartFactoryName.Text = Convert.ToString(dt.Rows[0]["CompanyName"]);
                        txtPartSingleprice.Text = Convert.ToString(dt.Rows[0]["SinglePrice"]);
                        //txtPartFactory.Text = Convert.ToString(dt.Rows[0]["SinglePrice"]);
                        TextBoxApplyDev.Text = Convert.ToString(dt.Rows[0]["ApplyDevices"]);
                        //txtStartDate.Text = Convert.ToDateTime(dt.Rows[0]["startdate"]).ToString("yyyy-MM-dd");
                        //txtEndDate.Text = Convert.ToDateTime(dt.Rows[0]["enddate"]).ToString("yyyy-MM-dd");
                        //txtMemo.Text = Convert.ToString(dt.Rows[0]["memo"]);
                        txtComment.Text = Convert.ToString(dt.Rows[0]["Comment"]);
                       
                        //txtPayMoney.Text = Convert.ToString(dt.Rows[0]["AccUserName"]);
                        //txtContent.SelectedValue = Convert.ToString(dt.Rows[0]["Comment"]);
                        //tdupload.InnerHtml = Convert.ToString(dt.Rows[0]["Devicefile"]);
                    }
//                    dt = ds.Tables[1];
//                    int iRowCount = dt.Rows.Count;
//                    StringBuilder sbu = new StringBuilder();
//                    for (int i = 0; i < iRowCount; i++)
//                    {
//                        sbu.Append(string.Format(@"<tr>
//                        <td align=""left"" width=""120"">
//                            <a href=""javascript:"" onclick=""insertdevice(this.parentNode.parentNode)"">+</a> <a href=""javascript:"" onclick=""removedevice(this.parentNode.parentNode)"">-</a><input style=""width: 92px"" type=""text"" onclick=""getdevicetype(this)"" value=""{0}"" /><input style=""width: 64px"" type=""hidden"" value=""{1}"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" value=""{2}"" /></td>
//                        <td width=""70"">
//                            <input  style=""width: 70px"" type=""text"" value=""{3}"" /></td>
//                        <td width=""150"">
//                            <input style=""width: 90px"" type=""text"" value=""{4}"" /></td>
//                    </tr>", dt.Rows[i]["typename"], dt.Rows[i]["typeid"], dt.Rows[i]["devicenum"], dt.Rows[i]["singleprice"], dt.Rows[i]["modelinfo"]));
//                    }
//                    ltrDevice.Text = Convert.ToString(sbu);
                }
            }
        }

        #endregion

        int m_Id
        {
            get
            {
                return Convert.ToInt32(Request["id"]);
            }
        }

        #region 获得设备

        //[Ajax.AjaxMethod]
        //public DataTable getSelectDeviceInfo(string sIds)
        //{
        //    BuyDevice buy = null;
        //    DataTable dt = null;
        //    try
        //    {
        //        buy = new BuyDevice();
        //        dt = buy.getResultDeviceInfo(sIds);
        //    }
        //    catch
        //    {
        //        dt = null;
        //    }
        //    finally
        //    {
        //        if (buy != null)
        //        {
        //            buy.Dispose();
        //        }
        //    }
        //    return dt;
        //} 

        #endregion

        #region 保存信息

        [Ajax.AjaxMethod(Ajax.HttpSessionStateRequirement.Read)]
        public bool saveData(int ipartid,string spartcode, string spartname, string spartdanwei, string spartspec, string spartweight, string spartcailiao, string spartfactory, string spartprice, string spartapplydev, string spartcomment, string sfiles)
        {
            DeviceInfo devinfo = null;
            bool bResult = false;
            spartcode = spartcode.Replace("'", "''");
            spartname = spartname.Replace("'", "''");
            spartdanwei = spartdanwei.Replace("'", "''");
            spartspec = spartspec.Replace("'", "''");
            spartweight = spartweight.Replace("'", "''");

            spartcailiao = spartcailiao.Replace("'", "''");
            spartfactory = spartfactory.Replace("'", "''");
            spartprice = spartprice.Replace("'", "''");
            spartapplydev = spartapplydev.Replace("'", "''");
            spartcomment = spartcomment.Replace("'", "''");
            try
            {
                devinfo = new DeviceInfo();

                //for (int i = 0; i < sDevices.Length; i++)
                //{
                //    sDevices[i] = sDevices[i].Replace("'", "''");
                //}
                bResult = devinfo.setDevPartInfo(ipartid, spartcode, spartname, spartdanwei, spartspec, spartweight, spartcailiao, spartfactory, spartprice, spartapplydev, spartcomment, sfiles);
            }
            catch
            {
                bResult = false;
            }
            finally
            {
                if (devinfo != null)
                {
                    devinfo.Dispose();
                }
            }
            return bResult;
        }

        #endregion
    }
}